<template>
	<view></view>
</template>
<script>
	let WebIM = require('../../../../../utils/WebIM')['default']
	let msgType = require('../../../msgtype')
	let disp = require('../../../../../utils/broadcast')
	let msgStorage = require('../../../msgstorage')
	export default {
		data() {
			return {}
		},
		props: {
			username: {
				type: Object,
				default: () => ({}),
			},
			chatType: {
				type: String,
				default: msgType.chatType.SINGLE_CHAT,
			},
			goodsInfo: { // 商品信息
				type: Object,
				default: () => ({}),
			},
			quoteInfo: { // 报价信息
				type: Object,
				default: () => ({}),
			}

		},
		methods: {
			sendCustom() {
				let me = this
				var id = WebIM.conn.getUniqueId() // 生成本地消息id
				var msg = new WebIM.message(msgType.CUSTOM, id) // 创建自定义消息
				var customEvent = 'orderMsg' // 创建自定义事件 比如订单消息orderMsg

				if (this.goodsInfo.id) {
					var customExts = this.goodsInfo // 自定义商品内容，key/value 需要 string 类型
				} else {
					var customExts = this.quoteInfo // 自定义报价内容，key/value 需要 string 类型
				}

				msg.set({
					from: WebIM.conn.user,
					to: me.getSendToParam(), // 接收消息对象（用户id） //接收消息的人
					customEvent,
					customExts,
					ext: {}, // 消息扩展
					chatType: me.chatType, // 设置单聊
					roomType: false,
					success: function(id, serverMsgId) {
						console.log('>>>>>>发送成功')
						disp.fire('em.chat.sendSuccess', id)
					},
					fail: function(e) {
						console.log('发送失败了', e);

					},
				})
				if (me.chatType == msgType.chatType.CHAT_ROOM) {
					msg.setGroup('groupchat')
				}
				WebIM.conn.send(msg.body)
				let obj = {
					msg: msg,
					type: msgType.CUSTOM,
				}

				console.log('obj>>', obj)
				me.saveSendMsg(obj)
			},
			getSendToParam() {
				return this.isGroupChat() ? this.username.groupId : this.username.your
			},
			isGroupChat() {
				return this.chatType == msgType.chatType.CHAT_ROOM
			},
			saveSendMsg(evt) {
				msgStorage.saveMsg(evt.msg, evt.type)
			},
		},
	}
</script>